//index.js
//获取应用实例
const app = getApp()
const MAX_LIMIT = 3;
Page({
  data: {
    //tabbar
    tabbar: {},
    lost: [],
    found: []
  },
  async _loadList() {
    wx.showLoading({
      title: '玩命加载中...',
    })
    const db = wx.cloud.database()
    const lost = await db.collection('list')
      .where({
        type: 'lost'
      })
      .limit(MAX_LIMIT)
      .orderBy('date', 'desc')
      .get()
    for (let o in lost.data) {
      lost.data[o].date = lost.data[o].date.getTime()
    }
    const found = await db.collection('list')
      .where({
        type: 'found'
      })
      .limit(MAX_LIMIT)
      .orderBy('date', 'desc')
      .get()
    for (let o in found.data) {
      found.data[o].date = found.data[o].date.getTime()
    }
    this.setData({
      lost: lost.data,
      found: found.data
    })
    wx.hideLoading()
    wx.stopPullDownRefresh()
  },
  onShow: function () {
    app.editTabbar()
    this._loadList()
    this.onGetOpenid();
  },
  onGetOpenid: function() {
    // 调用云函数
    let that = this;
    wx.cloud.callFunction({
      name: 'login',
      data: {

      }
    })
    .then(res => {
      console.log('[云函数] [login]: ', res)
      app.globalData.openid = res.result.openid;
      let openid = res.result.openid;
      console.log('openid');
      console.log(openid);
 
      
      
    }).catch(err => {
      console.error('[云函数] [login] 调用失败', err)
      wx.navigateTo({
        url: '../deployFunctions/deployFunctions',
      })
    })
  },
  onLoad: function () {},
  onPullDownRefresh(){
    this._loadList()
  },
})